var express = require('express');
var router = express.Router();
var Project = require('../database/project');//项目
var Manager = require('../database/manager');//管理人
var Owner = require('../database/owner');//原始受益人
var Accountant = require('../database/accountant');//会计师
var Lawyer = require('../database/lawyer');//律师
var ThirdCredential = require('../database/thirdCredential');//第三方认证机构
var Comment = require('../database/comment');//评价机构
const makeDir = require('make-dir');
const fs = require('fs');
router.get('/allProjects', function(req, res, next) {  //获取所有的创建项目
  Project.getAll((err,data)=>{ 
    res.send({back:"success",data:data});
    console.log(data) 
  }) 
});

router.get('/myProjects', function(req, res, next) {  //根据条件获取自己名下的项目
   let orgnization = req.session.user.orgnization;    //获取当前用户所属组织名称
   let role = req.session.user.role;                     //获取当前用户所属角色
   switch(role){
     case 'manager':
       Manager.prototype.myProjects((err,data)=>{ 
         if(!err){
            res.send({back:"success",data:data})
          }else{
            console.log(err)
          }
       });
     break;
     case 'owner':
       console.log("enter manager api")
       Owner.prototype.myProjects(orgnization,(err,data)=>{ //传入业主所属组织名称，获取所属组织名下项目
        if(!err){
            res.send({back:data})
            console.log(data)
          }else{
            console.log(err)
          }
       });
     break;
     case 'accountant':
       Accountant.prototype.myProjects(orgnization,(err,data)=>{
        if(!err){
            res.send({back:data})
            console.log(data)
          }else{
            console.log(err)
          }
       });
     break;
     case 'lawyer':
       Lawyer.prototype.myProjects(orgnization,(err,data)=>{
          if(!err){
            res.send({back:data})
            console.log(data)
          }else{
            console.log(err)
          }
       });
     break;
     case 'comment':
       Comment.prototype.myProjects(orgnization,(err,data)=>{
          if(!err){
            res.send({back:data})
            console.log(data)
          }else{
            console.log(err)
          }
       });
     break;
     case 'thirdCredential':
       ThirdCredential.prototype.myProjects(orgnization,(err,data)=>{
          if(!err){
            res.send({back:data})
            console.log(data)
          }else{
            console.log(err)
          }
       });
     break;
     default:
        console.log("default")
        res.send({back:null})
   }
});

router.post('/createProject',function(req,res,next){    //创建项目
  let project = req.body;
  let role = req.session.user.role;
  if(role !== 'manager'){
    res.send({back:"noRight"});
  }
  var newProject = new Project(project);
  Manager.prototype.createProj(newProject,(err)=>{
    if(!err){
      fs.exists('project', (exists) => {
         if (exists) {
            fs.open('project', 'r', (err, fd) => {
              readMyData(fd);
         });
     }else {
         console.error('myfile does not exist');
     }
});
      res.send({back:"success"})
    }else{
      console.log(err)
    }
  })
  
});

module.exports = router;